//
//  ESTBacklogViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/11/2.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTBacklogViewController.h"
#import "ESTActivityEffectiveView.h"
#define SelectedOptionViewHeight         40
#define NumberTipViewHeight              30
@interface ESTBacklogViewController ()
/**
 *  选择了哪一中选项(0为战败审核，1为线索分配)
 */
@property(nonatomic, assign) NSInteger selectedOption;
/**
 *  TableView
 */
@property(nonatomic, strong) TYZTableView *tableView;
/**
 *  战败审核的数据源
 */
@property(nonatomic, strong) NSMutableArray *failDataArray;
/**
 *  战败审核的总数
 */
@property(nonatomic, assign) NSInteger failTotalNumber;
/**
 *  线索分配的数据源
 */
@property(nonatomic, strong) NSMutableArray *clueDataArray;
/**
 *  线索分配的总数
 */
@property(nonatomic, assign) NSInteger clueTotalNumber;
/**
 *  请求页码
 */
@property(nonatomic, assign) NSInteger page_num;
/**
 *  显示数量的文本
 */
@property(nonatomic, strong) TYZLabel *numberLabel;
@end

@implementation ESTBacklogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView];
    [self setupBacklogSelectedView];
    [self setupNumberTipView];
    [self setupTableView];
}

#pragma mark - 设置UI
- (void)setupTitleView {
    TYZView *titleView = [[TYZView alloc] init];
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(DE_NavigationHeight);
    }];
    titleView.backgroundColor = ThemeColor;
    TYZLabel *titleLabel = [[TYZLabel alloc] init];
    [titleView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titleView);
        make.bottom.equalTo(titleView).offset(-10);
    }];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = [@"待办" S];
    titleLabel.font = [UIFont boldSystemFontOfSize:CoreInfo.fontSize * 1.2];
    titleLabel.textColor = TYZWhiteColor;
    
    TYZButton *rightButton = [[TYZButton alloc] init];
    [titleView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.right.equalTo(titleView).offset(-20);
    }];
    [rightButton setTitle:[@"选择" S] forState:UIControlStateNormal];
    [rightButton setTitleColor:TYZWhiteColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = TYZFont(CoreInfo.fontSize);
    [rightButton addTarget:self action:@selector(didClickRightButton) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setupBacklogSelectedView {
    TYZView *selectedView = [[TYZView alloc] init];
    [self.view addSubview:selectedView];
    [selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(DE_NavigationHeight);
        make.height.mas_equalTo(44);
    }];
    ESTActivityEffectiveView *leftView = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:[@"战败审核" S]];
    [selectedView addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(selectedView);
        make.width.mas_equalTo(kWindowWidth * 0.5);
    }];
    leftView.isSelected = YES;
    ESTActivityEffectiveView *rightView = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:[@"线索分配" S]];
    [selectedView addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(selectedView);
        make.width.mas_equalTo(kWindowWidth * 0.5);
    }];
    WEAKSELF
    __weak typeof(leftView) weakLeftView = leftView;
    __weak typeof(rightView) weakRightView = rightView;
    [leftView setDidCliclCoverBlock:^{
        weakSelf.selectedOption = 0;
        weakLeftView.isSelected = YES;
        weakRightView.isSelected = NO;
    }];
    [rightView setDidCliclCoverBlock:^{
        weakSelf.selectedOption = 1;
        weakLeftView.isSelected = NO;
        weakRightView.isSelected = YES;
    }];
    [selectedView addLine];
}
- (void)setupNumberTipView {
    TYZView *tipView = [[TYZView alloc] init];
    [self.view addSubview:tipView];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(DE_NavigationHeight + SelectedOptionViewHeight);
        make.height.mas_equalTo(NumberTipViewHeight);
    }];
    tipView.backgroundColor = ESTBackgroundColor;
    TYZLabel *label = [[TYZLabel alloc] init];
    [tipView addSubview:label];
    self.numberLabel = label;
    label.textColor = TYZLightMColor;
    label.font = TYZFont(CoreInfo.fontSize * 0.8);
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(tipView);
        make.left.equalTo(tipView).offset(10);
    }];
    if (self.selectedOption) {
        label.text = [NSString stringWithFormat:@"%@%ld%@", [@"共有" S], (long)self.failTotalNumber, [@"待审核" S]];
    } else {
        label.text = [NSString stringWithFormat:@"%@%ld%@", [@"共有" S], (long)self.clueTotalNumber, [@"待分配" S]];
    }
}
- (void)setupTableView {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(DE_NavigationHeight + SelectedOptionViewHeight + NumberTipViewHeight);
    }];
    [self setupTableViewHeader];
}
- (void)pullDownRefreshData {
    self.page_num = 1;
    [self.failDataArray removeAllObjects];
    [self acquireData];
}
- (void)pullUpLoadMore {
    self.page_num++;
    [self acquireData];
}
- (void)setupTableViewHeader {
    WEAKSELF
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [weakSelf pullDownRefreshData];
    }];
}
- (void)setupTableViewFooter {
    WEAKSELF
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf pullUpLoadMore];
    }];
}
#pragma mark - actions
- (void)didClickRightButton {
    
}
- (void)acquireData {
    
}
#pragma mark - setter & getter
//- (TYZTableView *)tableView {
//    if (_tableView == nil) {
//        _tableView = [[TYZTableView alloc] init];
//        _tableView.delegate = self;
//        _tableView.dataSource = self;
//    }
//    return _tableView;
//}
- (void)setSelectedOption:(NSInteger)selectedOption {
    _selectedOption = selectedOption;
    if (selectedOption) {
        self.numberLabel.text = [NSString stringWithFormat:@"%@%ld%@", [@"共有" S], (long)self.failTotalNumber, [@"待审核" S]];
    } else {
        self.numberLabel.text = [NSString stringWithFormat:@"%@%ld%@", [@"共有" S], (long)self.clueTotalNumber, [@"待分配" S]];
    }
}
@end
